package com.cityfood.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cityfood.mapper.CfStationMapper;
import com.cityfood.pojo.CfCity;
import com.cityfood.pojo.CfStation;
import com.cityfood.pojo.CfStationExample;
import com.cityfood.pojo.CfStationExample.Criteria;
import com.cityfood.service.StationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class StationServiceImpl implements StationService {

	@Autowired
	private CfStationMapper stationMapper;

	@Override
	public List<CfStation> getStationByCityId(Integer cityId) {
		CfStationExample stationExample = new CfStationExample();
		Criteria criteria = stationExample.createCriteria();
		criteria.andCityIdEqualTo(cityId);
		List<CfStation> stations = stationMapper.selectByExample(stationExample);
		return stations;
	}
	
	@Override
	public PageInfo<CfStation> getStationByCityIdAndPage(List<Integer> cityIds,String name,Integer pageNum) {
		
		//设置分页的页数和行数
		PageHelper.startPage(pageNum, 20);
		
		CfStationExample stationExample = new CfStationExample();
		Criteria criteria = stationExample.createCriteria();
		criteria.andCityIdIn(cityIds);
		criteria.andNameLike("%"+name+"%");
		List<CfStation> stations = stationMapper.selectByExample(stationExample);
		//5.获取分页信息
		PageInfo<CfStation> info = new PageInfo<>(stations);
		return info;
	}

	@Override
	public int insertStation(CfStation station) {
		int i = stationMapper.insertSelective(station);
		return i;
	}

	@Override
	public int updateStation(CfStation station) {
		int i = stationMapper.updateByPrimaryKeySelective(station);
		return i;
	}

	@Override
	public int deletetStation(CfStation station) {
		station.setDisplay((byte)0);
		int i = stationMapper.updateByPrimaryKeySelective(station);
		return i;
	}

	@Override
	public CfStation getStationById(Integer id) {
		CfStation station = stationMapper.selectByPrimaryKey(id);
		return station;
	}

	@Override
	public List<CfStation> getAllStationLikeName(String name) {
		CfStationExample stationExample = new CfStationExample();
		Criteria criteria = stationExample.createCriteria();
		criteria.andNameLike("%"+name+"%");
		List<CfStation> stations = stationMapper.selectByExample(stationExample);
		return stations;
	}

}
